﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using IDAL;
using Models;

namespace DAL
{
    public class ContactInfoDAL:IContactInfo
    {
        public const string SQL_GET_CONTACTINFO = @"select * from ContactInfo";
        public DataBase DB = new DataBase();

        public List<ContactInfo> GetContactInfo()
        {
            List<ContactInfo> list = new List<ContactInfo>();
            DB.DatabaseConnection();
            SqlDataReader reader = DB.GetdataRecords(SQL_GET_CONTACTINFO);                
            while (reader.Read())
            {
                ContactInfo contactInfo = new ContactInfo();
                ReadtoContactInfo(reader, contactInfo);
                list.Add(contactInfo);
            }
            if (list.Count() == 0) {
                list.Add(new ContactInfo());
            }
            DB.CloseDB();
            return list;
        }

        public void ReadtoContactInfo(SqlDataReader reader, ContactInfo contactInfo)
        {
            contactInfo.CName = reader["CName"] is DBNull?"":reader["CName"].ToString();
            contactInfo.EName = reader["EName"] is DBNull?"":reader["EName"].ToString();
            contactInfo.Telephone =reader["Telephone"] is DBNull?"":reader["Telephone"].ToString();
            contactInfo.EmailAdress =reader["EmailAdress"] is DBNull?"":reader["EmailAdress"].ToString();
            contactInfo.CAdress =reader["CAdress"] is DBNull?"":reader["CAdress"].ToString();
            contactInfo.EAdress =reader["EAdress"] is DBNull?"":reader["EAdress"].ToString();
            contactInfo.CCopyRight =reader["CCopyRight"] is DBNull?"":reader["CCopyRight"].ToString();
            contactInfo.ECopyRight =reader["ECopyRight"] is DBNull?"":reader["ECopyRight"].ToString();
            contactInfo.BeiAn =reader["BeiAn"] is DBNull?"":reader["BeiAn"].ToString();
        }

        public int UpdateContactInfo(ContactInfo contact)
        {
            int isSuccess = -1;
            string sql = @"UPDATE ContactInfo SET 
                                   [CName] = '" + contact.CName + @"'
                                  ,[EName] = '" + contact.EName + @"'
                                  ,[Telephone] = '" + contact.Telephone + @"'
                                  ,[EmailAdress] = '" + contact.EmailAdress + @"'
                                  ,[CAdress] = '" + contact.CAdress + @"'
                                  ,[EAdress] = '" + contact.EAdress + @"'
                                  ,[CCopyRight] = '" + contact.CCopyRight + @"'
                                  ,[ECopyRight] = '" + contact.ECopyRight + @"'
                                  ,[BeiAn] = '" + contact.BeiAn + @"'            ";
            isSuccess = DB.UpdateRecords(sql);
            return isSuccess;
        }
        public int DeleteContactInfo()
        {
            int isSuccess = -1;
            string sql = "DELETE FROM ContactInfo ";
            isSuccess = DB.UpdateRecords(sql);
            return isSuccess;
        }
        public int AddContactInfo(ContactInfo contact)
        {
            int isSuccess = -1;
            string sql = @"INSERT INTO ContactInfo([CName],[EName],[Telephone],[EmailAdress],[CAdress],[EAdress],[CCopyright],[ECopyright],[Beian]) 
                        VALUES('" + contact.CName + "','" + contact.EName + "','" + contact.Telephone + "','" + contact.EmailAdress + "','" + contact.CAdress + "','" + contact.EAdress + "','" + contact.CCopyRight + "','" + contact.ECopyRight + "','" + contact.BeiAn + "')";
            isSuccess = DB.UpdateRecords(sql);
            return isSuccess;
        }
    }
}
